package com.youjie.demo.util;

import cn.hutool.core.codec.Base64;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JPushUtil {

  private static String authorization = "Basic "+ Base64.encode("fb683435a10c1107a718930f:be0739d895b011f67a0a6591");


  /**
   * 推送消息
   * @param msg
   * @return
   */
  public static JSONObject push(String msg) {
    // 请求参数
    Map<String, Object> map = new HashMap<>();

    // 通知的用户
    HashMap<Object, Object> audience = new HashMap<>();
    List<String> ids = new ArrayList<>();
    audience.put("registration_id",ids);
    ids.add("120c83f760d644505fc");

    // 消息
    Map<String, Object> alert = new HashMap<>();
    alert.put("alert",msg);

    map.put("platform","all");
//    map.put("audience","all");  广播
    map.put("audience",audience);
    map.put("notification",alert);

    HttpRequest request = HttpRequest.post("https://api.jpush.cn/v3/push")
        .header("Authorization", authorization)
        .body(JSONUtil.toJsonStr(map));
    String body = request.execute().body();

    System.out.println(body);
    JSONObject result = JSONUtil.parseObj(body);
    return result;
  }

}
